1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<bits/stdc++.h> using namespace std; typedef long long s64;
const int ONE = 1001;
int T; int a[8][8],Step,Vx,Vy; char ch[8]; bool PD; int dx[]={1,2,-1,-2,1,2,-1,-2}; int dy[]={2,1,2,1,-2,-1,-2,-1}; int Goal[6][6]= { {0,0,0,0,0,0}, {0,1,1,1,1,1}, {0,0,1,1,1,1}, {0,0,0,2,1,1}, {0,0,0,0,0,1}, {0,0,0,0,0,0} };
inline int get() { int res=1,Q=1; char c; while( (c=getchar())<48 || c>57) if(c=='-')Q=-1; if(Q) res=c-48; while((c=getchar())>=48 && c<=57) res=res*10+c-48; return res*Q; }
int Evaluation() { int res = 0; for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) if(a[i][j] != Goal[i][j]) res++; return res; }
void Dfs(int T,int x,int y) { if(PD) return; if(T == Step) { PD = !Evaluation(); return; }
for(int i=0;i<8;i++) { int Nx = x+dx[i], Ny = y+dy[i]; if(!(1<=Nx && Nx<=5 && 1<=Ny && Ny<=5)) continue; swap(a[x][y], a[Nx][Ny]); if(Evaluation() + T <= Step) Dfs(T+1, Nx,Ny); swap(a[x][y], a[Nx][Ny]); } }
void Solve() { for(int i=1;i<=5;i++) { scanf("%s",ch+1); for(int j=1;j<=5;j++) { if(ch[j] == '*') {a[i][j] = 2, Vx=i,Vy=j;} else a[i][j] = ch[j]-'0'; } }
PD=0; for(Step=1;Step<=15;Step++) { Dfs(0,Vx,Vy); if(PD) break; }
printf("%d\n",PD==1 ? Step:-1); }
int main() { T=get(); while(T--) Solve(); }
|